Note: Click the following button to show or hide the code.
from IPython.display import HTML
HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
code_show = !code_show
}
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Show/Hide Code"></form>''')
Coronavirus, which has affected all our lives in unimaginable ways, needs no introduction to anyone. Covid-19 has changed the way we interact, our habits, and even our minds. It has brought all of us down on our knees and forced us to make ourselves quarantined in our homes. I took this quarantine time as a learning opportunity and thought of building a data visualization platform which helps telling the story of how this virus has been unfolding since it has started. There are already few similar platforms out there but here I have tried to make it more elaborate and present some additional useful plots and insights.
To build an interactive data visualization platform which makes it intuitive and easier to track different kinds of trends and statistics
The dataset relating to cases are taken from the Johns Hopkins Covid-19 dataset, and the dataset relating to tests are from Our World in Data COVID-19 dataset.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import ticker
import pycountry_convert as pc
import folium, os
from datetime import datetime,date
from scipy.interpolate import make_interp_spline, BSpline
import plotly.express as px
from plotly.offline import init_notebook_mode, iplot
import plotly.graph_objs as go
import plotly.offline as py
from plotly.offline import download_plotlyjs,init_notebook_mode,plot,iplot
from plotly.subplots import make_subplots
from IPython.core.display import display, HTML, Markdown as md
from tabulate import tabulate
%matplotlib inline
import warnings
warnings.filterwarnings('ignore')
py.init_notebook_mode()
%%javascript
IPython.OutputArea.prototype._should_scroll = function(lines) {
return false;
}
#%%capture
# this is used to not print any cell output.
#comment out first line to print the output (enable print statement).
df_confirmed = pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv')
df_deaths = pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv')
df_recovered = pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv')
test_df= pd.read_csv("https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/testing/covid-testing-all-observations.csv")
pop_df= pd.read_csv("../Data/Population_Data_Country.csv")
test_df['Date']= pd.to_datetime(test_df['Date'], format= "%Y/%m/%d")
df_confirmed.shape, df_deaths.shape, df_recovered.shape, test_df.shape, pop_df.shape;
last_updated_date= df_confirmed.columns[-1]
second_last_updated_date= df_confirmed.columns[-2]
# display(Markdown("Latest data is available till" +" " + last_updated_date))
display(md("__Last updated:__ {}".format(last_updated_date)))
last_updated_date,second_last_updated_date; #semicolon is being used to not print as cell output
df_confirmed = df_confirmed.rename(columns={"Province/State":"state","Country/Region": "country"})
df_deaths = df_deaths.rename(columns={"Province/State":"state","Country/Region": "country"})
df_recovered = df_recovered.rename(columns={"Province/State":"state","Country/Region": "country"})
# Changing the conuntry names as required by pycountry_convert Lib
df_confirmed.loc[df_confirmed['country'] == "US", "country"] = "USA"
df_deaths.loc[df_deaths['country'] == "US", "country"] = "USA"
df_recovered.loc[df_recovered['country'] == "US", "country"] = "USA"
df_confirmed.loc[df_confirmed['country'] == 'Korea, South', "country"] = 'South Korea'
df_deaths.loc[df_deaths['country'] == 'Korea, South', "country"] = 'South Korea'
df_recovered.loc[df_recovered['country'] == 'Korea, South', "country"] = 'South Korea'
df_confirmed.loc[df_confirmed['country'] == 'Taiwan*', "country"] = 'Taiwan'
df_deaths.loc[df_deaths['country'] == 'Taiwan*', "country"] = 'Taiwan'
df_recovered.loc[df_recovered['country'] == 'Taiwan*', "country"] = 'Taiwan'
df_confirmed.loc[df_confirmed['country'] == 'Congo (Kinshasa)', "country"] = 'Democratic Republic of the Congo'
df_deaths.loc[df_deaths['country'] == 'Congo (Kinshasa)', "country"] = 'Democratic Republic of the Congo'
df_recovered.loc[df_recovered['country'] == 'Congo (Kinshasa)', "country"] = 'Democratic Republic of the Congo'
df_confirmed.loc[df_confirmed['country'] == "Cote d'Ivoire", "country"] = "Côte d'Ivoire"
df_deaths.loc[df_deaths['country'] == "Cote d'Ivoire", "country"] = "Côte d'Ivoire"
df_recovered.loc[df_recovered['country'] == "Cote d'Ivoire", "country"] = "Côte d'Ivoire"
df_confirmed.loc[df_confirmed['country'] == "Reunion", "country"] = "Réunion"
df_deaths.loc[df_deaths['country'] == "Reunion", "country"] = "Réunion"
df_recovered.loc[df_recovered['country'] == "Reunion", "country"] = "Réunion"
df_confirmed.loc[df_confirmed['country'] == 'Congo (Brazzaville)', "country"] = 'Republic of the Congo'
df_deaths.loc[df_deaths['country'] == 'Congo (Brazzaville)', "country"] = 'Republic of the Congo'
df_recovered.loc[df_recovered['country'] == 'Congo (Brazzaville)', "country"] = 'Republic of the Congo'
df_confirmed.loc[df_confirmed['country'] == 'Bahamas, The', "country"] = 'Bahamas'
df_deaths.loc[df_deaths['country'] == 'Bahamas, The', "country"] = 'Bahamas'
df_recovered.loc[df_recovered['country'] == 'Bahamas, The', "country"] = 'Bahamas'
df_confirmed.loc[df_confirmed['country'] == 'Gambia, The', "country"] = 'Gambia'
df_deaths.loc[df_deaths['country'] == 'Gambia, The', "country"] = 'Gambia'
df_recovered.loc[df_recovered['country'] == 'Gambia, The', "country"] = 'Gambia'
#%%capture
#only few countries has data relating multiple states/provinces.
df_confirmed["country"].value_counts(); #remove semicolon to print
As we see below, there are no missing values except in state column. Since, we are dealing on country level and not on state level, imputation is not needed.
#%%capture
display(md("__Number of missing values in confirmed cases data__:"))
print(df_confirmed.isna().sum()[df_confirmed.columns[df_confirmed.isna().sum()!=0].tolist()].to_string())
display(md("__Number of missing values in deaths data__:"))
print(df_deaths.isna().sum()[df_deaths.columns[df_deaths.isna().sum()!=0].tolist()].to_string())
display(md("__Number of missing values in recovered cases data__:"))
print(df_recovered.isna().sum()[df_recovered.columns[df_recovered.isna().sum()!=0].tolist()].to_string())
#replace all nans with empty string
df_confirmed = df_confirmed.replace(np.nan, '', regex=True)
df_deaths = df_deaths.replace(np.nan, '', regex=True)
df_recovered = df_recovered.replace(np.nan, '', regex=True)
#From 6th column we have numbers corrsponding to different dates.
# df_confirmed.iloc[:,5:].fillna(0, inplace=True)
# df_recovered.iloc[:,5:].fillna(0, inplace=True)
# df_deaths.iloc[:,5:].fillna(0, inplace=True)
#Inserting continent column
countries_confirmed = np.asarray(df_confirmed["country"])
countries_deaths = np.asarray(df_deaths["country"])
countries_recovered = np.asarray(df_recovered["country"])
# Continent_code to Continent_names
continents = {
'NA': 'North America',
'SA': 'South America',
'AS': 'Asia',
'OC': 'Australia',
'AF': 'Africa',
'EU' : 'Europe',
'na' : 'Others'
}
# Defininng Function for getting continent code for country.
def country_to_continent_code(country):
try:
return pc.country_alpha2_to_continent_code(pc.country_name_to_country_alpha2(country))
except :
return 'na'
#Collecting Continent Information
df_confirmed.insert(2,"continent", [continents[country_to_continent_code(country)] for country in countries_confirmed[:]])
df_deaths.insert(2,"continent", [continents[country_to_continent_code(country)] for country in countries_deaths[:]])
df_recovered.insert(2,"continent", [continents[country_to_continent_code(country)] for country in countries_recovered[:]] )
%%capture
# this is used to not print any cell output.
#comment out first line to print the output (enable print statement).
# getting country wise data
#here we are sorting first, because we want the Lat and long for a country corresponding to its most affected (most cases on last updated date) state.
#for some countries there are multiple states, that is why it needs to be done
df_confirmed_sorted_total= df_confirmed.sort_values(by= last_updated_date, ascending= False)
df_recovered_sorted_total= df_recovered.sort_values(by= last_updated_date, ascending= False)
df_deaths_sorted_total= df_deaths.sort_values(by= last_updated_date, ascending= False)
#Adding all the states numbers to get the numbers at country level
confirmed_cases_country_1 = df_confirmed_sorted_total.groupby(["country"]).sum().drop(['Lat','Long'],axis =1).reset_index()
recovered_cases_country_1 = df_recovered_sorted_total.groupby(["country"]).sum().drop(['Lat','Long'],axis =1).reset_index()
deaths_country_1 = df_deaths_sorted_total.groupby(["country"]).sum().drop(['Lat','Long'],axis =1).reset_index()
# active_cases_country_1 = df_active.groupby(["country"]).sum().drop(['Lat','Long'],axis =1).reset_index()
#getting the Lat and Long corresponding to most affected state in a country.
#although it is better to get the Lat and Long info corresponding to the capital city. but for that we have to import another dataset.
confirmed_cases_country_2 = df_confirmed_sorted_total.groupby(["country"])['Lat', 'Long'].first().reset_index()
recovered_cases_country_2 = df_recovered_sorted_total.groupby(["country"])['Lat', 'Long'].first().reset_index()
deaths_country_2 = df_deaths_sorted_total.groupby(["country"])['Lat', 'Long'].first().reset_index()
# active_cases_country_2 = df_active.groupby(["country"])['Lat', 'Long'].mean().reset_index()
confirmed_cases_country= confirmed_cases_country_2.merge(confirmed_cases_country_1, how='inner', on='country')
recovered_cases_country= recovered_cases_country_2.merge(recovered_cases_country_1, how='inner', on='country')
deaths_country= deaths_country_2.merge(deaths_country_1, how='inner', on='country')
# active_cases_country= active_cases_country_2.merge(active_cases_country_1, how='inner', on='country')
#here again we sort but by country, because we want to find df for active cases by subtracting later
confirmed_cases_country= confirmed_cases_country.sort_values(by= "country", ascending= True)
recovered_cases_country= recovered_cases_country.sort_values(by= "country", ascending= True)
deaths_country= deaths_country.sort_values(by= "country", ascending= True)
# active_cases_country= active_cases_country.sort_values(by= "country", ascending= True)
# recovered_cases_country_1 = df_recovered.groupby(["country"]).sum().drop(['Lat','Long'],axis =1)
# deaths_country_1 = df_deaths.groupby(["country"]).sum().drop(['Lat','Long'],axis =1)
# active_cases_country_1 = df_active.groupby(["country"]).sum().drop(['Lat','Long'],axis =1)
# confirmed_cases.name = "Confirmed Cases"
# recovered_cases.name = "Recovered Cases"
# deaths.name = "Deaths Reported"
# active_cases.name = "Active Cases"
# df_countries_cases = pd.DataFrame([confirmed_cases,recovered_cases,deaths,active_cases]).transpose()
# getting continent wise data the same way we did for getting country level data
confirmed_cases_continent_1 = df_confirmed_sorted_total.groupby(["continent"]).sum().drop(['Lat','Long'],axis =1).reset_index()
recovered_cases_continent_1 = df_recovered_sorted_total.groupby(["continent"]).sum().drop(['Lat','Long'],axis =1).reset_index()
deaths_continent_1 = df_deaths_sorted_total.groupby(["continent"]).sum().drop(['Lat','Long'],axis =1).reset_index()
# active_cases_continent_1 = df_active.groupby(["continent"]).sum().drop(['Lat','Long'],axis =1).reset_index()
confirmed_cases_continent_2 = df_confirmed_sorted_total.groupby(["continent"])['Lat', 'Long'].first().reset_index()
recovered_cases_continent_2 = df_recovered_sorted_total.groupby(["continent"])['Lat', 'Long'].first().reset_index()
deaths_continent_2 = df_deaths_sorted_total.groupby(["continent"])['Lat', 'Long'].first().reset_index()
# active_cases_continent_2 = df_active.groupby(["continent"])['Lat', 'Long'].mean().reset_index()
confirmed_cases_continent= confirmed_cases_continent_2.merge(confirmed_cases_continent_1, how='inner', on='continent')
recovered_cases_continent= recovered_cases_continent_2.merge(recovered_cases_continent_1, how='inner', on='continent')
deaths_continent= deaths_continent_2.merge(deaths_continent_1, how='inner', on='continent')
# active_cases_continent= active_cases_continent_2.merge(active_cases_continent_1, how='inner', on='continent')
confirmed_cases_continent= confirmed_cases_continent.sort_values(by= last_updated_date, ascending= False)
recovered_cases_continent= recovered_cases_continent.sort_values(by= last_updated_date, ascending= False)
deaths_continent= deaths_continent.sort_values(by= last_updated_date, ascending= False)
# active_cases_continent= active_cases_continent.sort_values(by= "continent", ascending= True)
# confirmed_cases.name = "Confirmed Cases"
# recovered_cases.name = "Recovered Cases"
# deaths.name = "Deaths Reported"
# active_cases.name = "Active Cases"
# df_continents_cases = pd.DataFrame([confirmed_cases,recovered_cases,deaths,active_cases]).transpose()
print(confirmed_cases_country.shape, recovered_cases_country.shape, deaths_country.shape);
print(confirmed_cases_continent.shape, recovered_cases_continent.shape, deaths_continent.shape);
%%capture
#Calculate active cases
active_cases_country = confirmed_cases_country.copy()
active_cases_country.iloc[:,3:] = (active_cases_country.iloc[:,3:] - recovered_cases_country.iloc[:,3:] - deaths_country.iloc[:,3:])#this makes all numeric columns to object type. that's why followin line is used to convert it back to numeric type.
active_cases_country.iloc[:,3:]= active_cases_country.iloc[:,3:].fillna(0)
active_cases_continent = confirmed_cases_continent.copy()
active_cases_continent.iloc[:,3:] = (active_cases_continent.iloc[:,3:] - recovered_cases_continent.iloc[:,3:] - deaths_continent.iloc[:,3:])#this makes all numeric columns to object type. that's why followin line is used to convert it back to numeric type.
active_cases_continent.iloc[:,3:]= active_cases_continent.iloc[:,3:].fillna(0)
# df_active= df_active.dropna(axis= 0, thresh= 68)
# df_active.iloc[:,5:]= df_active.iloc[:,5:].astype(int)
# df_active.iloc[:,5:]= df_active.iloc[:,5:].apply(lambda x: pd.to_numeric(x))
active_cases_country.shape, active_cases_continent.shape;
#here we find sorted version of all these DataFrames in terms of numbers
confirmed_cases_country_sorted= confirmed_cases_country.sort_values(by= last_updated_date, ascending= False)
recovered_cases_country_sorted= recovered_cases_country.sort_values(by= last_updated_date, ascending= False)
deaths_country_sorted= deaths_country.sort_values(by= last_updated_date, ascending= False)
active_cases_country_sorted= active_cases_country.sort_values(by= last_updated_date, ascending= False)
#here we prepare data regarding tests
test_df["country"]= test_df["Entity"].str.split("-").apply(lambda x: x[0].strip(' '))
test_df_fil= test_df[['country', 'Date', 'Cumulative total', 'Daily change in cumulative total', \
'Cumulative total per thousand', 'Daily change in cumulative total per thousand']]
#There are more than one entries for a particular (country, date) combination. thats why we need to group and select the max one on a particular day
test_df_grp= test_df_fil.groupby(by=["country", "Date"])["Cumulative total", "Cumulative total per thousand"].max()
test_df_grp= test_df_grp.reset_index()
test_df_grp= test_df_grp.dropna(how="any")
test_df_grp= test_df_grp.sort_values(by= ["country", "Date"], ascending= True)
prev_country= test_df_grp.iloc[0,0]
curr_country= test_df_grp.iloc[0,0]
test_df_grp["New tests"]= 0
for i in range(1,test_df_grp.shape[0]):
curr_country= test_df_grp.iloc[i,:]["country"]
if(curr_country==prev_country):
test_df_grp.iloc[i,-1]= test_df_grp.iloc[i,:]["Cumulative total"]-test_df_grp.iloc[i-1,:]["Cumulative total"]
else:
test_df_grp.iloc[i,-1]= 0
prev_country= curr_country
#making the country name uniform across cases and test dataframes
test_df_grp.loc[test_df_grp["country"]== 'Czech Republic', "country"] = "Czechia"
test_df_grp.loc[test_df_grp["country"]== 'United States', "country"] = "USA"
#converting number of tests per thousand to tests per million
test_df_grp["Cumulative total per thousand"]= 1000*test_df_grp["Cumulative total per thousand"].astype(int)
# test_df_grp["Cumulative total per thousand"]= test_df_grp["Cumulative total per thousand"].astype(int)
test_df_grp= test_df_grp.rename(columns={"Cumulative total per thousand":"Total tests per million", "Cumulative total":"Total tests" })
country_list_test= np.unique(test_df_grp["country"])
n_countries_test= country_list_test.shape[0]
display(md("__Note:__ We have data relating to cases for __{}__ countries but data relating to tests are present for only __{}__ countries.".format(confirmed_cases_country.shape[0], n_countries_test)))
test_df_grp.shape;
#first merge country-wise and population data and then with test data
test_df_grp_country= test_df_grp.groupby(by="country")["Total tests", "Total tests per million"].max().reset_index()
confirmed_cases_pop= confirmed_cases_country.merge(pop_df, how="left", on= "country")
confirmed_cases_tests_pop= confirmed_cases_pop.merge(test_df_grp_country, how="left", on= "country")
table_stat_country= pd.DataFrame()
table_stat_country["Country"]= confirmed_cases_tests_pop["country"]
table_stat_country["Total cases"]= confirmed_cases_country[last_updated_date]
table_stat_country["New cases"]= confirmed_cases_country[last_updated_date]-confirmed_cases_country[second_last_updated_date]
table_stat_country["Total deaths"]= deaths_country[last_updated_date]
table_stat_country["New deaths"]= deaths_country[last_updated_date]- deaths_country[second_last_updated_date]
table_stat_country["Total active"]= active_cases_country[last_updated_date]
table_stat_country["Total recovered"]= recovered_cases_country[last_updated_date]
total_world_stat= table_stat_country.sum(axis=0)
total_world_stat["Country"]= "World"
table_stat_country["Total cases per million"]= round(confirmed_cases_country[last_updated_date]/(confirmed_cases_pop["population"]/1000000),2)
table_stat_country["Total deaths per million"]= round(deaths_country[last_updated_date]/(confirmed_cases_pop["population"]/1000000),2)
table_stat_country["Total tests"]= confirmed_cases_tests_pop["Total tests"]
table_stat_country["Total tests per million"]= confirmed_cases_tests_pop["Total tests per million"]
total_world_stat["Total cases per million"]= round(total_world_stat["Total cases"]/(confirmed_cases_pop["population"].sum()/1000000),2)
total_world_stat["Total deaths per million"]= round(total_world_stat["Total deaths"]/(confirmed_cases_pop["population"].sum()/1000000),2)
table_stat_country= table_stat_country.append(total_world_stat, ignore_index= True)
table_stat_country= table_stat_country.sort_values(by= "Total cases", ascending= False)
table_stat_country.reset_index(inplace= True, drop= True)
table_stat_country = table_stat_country.replace(np.nan, '', regex=True)
# try:
# table_stat_country["Total cases per million "]= confirmed_cases_country[last_updated_date]/(confirmed_cases_pop["population"]/1000000)
# table_stat_country["Total deaths per million "]= deaths_country[last_updated_date]/(confirmed_cases_pop["population"]/1000000)
# except:
# table_stat_country["Total cases per million "]=''
# table_stat_country["Total deaths per million "]=''
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
# display(HTML(table_stat_country.to_html(index= False, justify= "center")))
display(HTML(table_stat_country.to_html( justify= "center")))
# table_stat_country.style.background_gradient(cmap='Reds', vmax= table_stat_country.iloc[1,1])
# print(tabulate(table_stat_country))
confirmed_cases_country_total= confirmed_cases_country.iloc[:,-1].sum()
recovered_cases_country_total= recovered_cases_country.iloc[:,-1].sum()
deaths_country_total= deaths_country.iloc[:,-1].sum()
active_cases_country_total= active_cases_country.iloc[:,-1].sum()
country_total_df= pd.DataFrame({'Confirmed cases': [confirmed_cases_country_total], 'Recovered cases':[recovered_cases_country_total], \
'Deaths Reported': [deaths_country_total], "Active Cases": [active_cases_country_total]})
country_total_df_transpose= country_total_df.transpose().reset_index()
country_total_df_transpose.columns= ["Case type", "Number of cases"]
# country_total_df_transpose
fig = make_subplots(rows=1, cols=2,
specs=[[{"type": "xy"},{"type": "domain"}]])
trace1 = go.Bar(
x = country_total_df_transpose['Case type'],
y = country_total_df_transpose["Number of cases"],
name = "Bar plot",
marker = dict(color = ['#4D9DE0', '#3DB876', '#E15554','#E6C74C'], opacity= 0.7,
line=dict(color='rgb(0,0,0)',width=1.5)),
text = country_total_df_transpose["Number of cases"],
textposition='outside', hoverinfo= "x+y", showlegend= False)
trace2= go.Pie(
values= country_total_df_transpose.iloc[1:,:]["Number of cases"],
labels= country_total_df_transpose.iloc[1:,:]['Case type'],
name= "",
domain=dict(x=[0.5, 1.0], y= [0.2, 0.8]),
textinfo='percent', textfont_size=15,
hoverinfo= "label+value",
hole= .4, opacity= 0.9,
marker_colors=['#3DB876', '#E15554','#E6C74C'],
# marker = dict(line=dict(color='rgb(0,0,0)',width=1.5)),
textposition='outside')
fig.add_trace(trace1, row=1, col=1)
fig.add_trace(trace2, row=1, col=2)
fig.update_yaxes(title_text="<b>Number of cases</b>", row=1, col=1)
# fig.update_traces(marker_color='#46cdcf', opacity=0.8, textposition='outside')
fig.update_layout(width=1200, height= 500, title_text= '<b>Global distribution of cases</b>',
title_x=0.4, title_y= 0.9, plot_bgcolor='rgba(240,240,240,0.8)')
iplot(fig)
confirmed_cases_country_bool= confirmed_cases_country.drop(['Lat', 'Long'], axis=1)
# confirmed_cases_country_bool.iloc[:,1:]= confirmed_cases_country_bool.iloc[:,1:][confirmed_cases_country_bool.iloc[:,1:]!=0.0]
confirmed_cases_country_bool.set_index("country", inplace= True)
confirmed_cases_country_bool= confirmed_cases_country_bool!=0
n_country_confirmed_cases= confirmed_cases_country_bool.sum(axis=0)
n_country_confirmed_cases= n_country_confirmed_cases.reset_index()
n_country_confirmed_cases.columns= ['Date', 'Number of countries affected']
trace1 = go.Scatter(
x = n_country_confirmed_cases["Date"],
y = n_country_confirmed_cases["Number of countries affected"],
mode = "lines+markers",
name = "",
marker = dict(color = 'rgba(70, 174, 160, 1)')
# text= n_country_confirmed_cases["Number of countries affected"]
)
data = [trace1]
layout = dict(title = '<b>Number of countries affected over time</b>',
xaxis= dict(title= '<b>Date</b>', tickangle= -45), yaxis= dict(title= '<b>Number of countries</b>'),
title_x=0.5, title_y= 0.9, plot_bgcolor='rgba(240,240,240,0.8)'
)
fig = dict(data = data, layout = layout)
iplot(fig)
# width=1200, height= 1000, title_text= 'Distribution of all continents till ' + last_updated_date,
# plot_bgcolor='rgb(250, 242, 242)'
confirmed_cases_country_sum= confirmed_cases_country.drop(['Lat', 'Long'], axis=1)
confirmed_cases_country_sum.set_index("country", inplace= True)
confirmed_cases_country_sum= confirmed_cases_country_sum.sum(axis=0)
confirmed_cases_country_sum= confirmed_cases_country_sum.reset_index()
confirmed_cases_country_sum.columns= ['Date', 'Number of confirmed cases']
deaths_country_sum= deaths_country.drop(['Lat', 'Long'], axis=1)
deaths_country_sum.set_index("country", inplace= True)
deaths_country_sum= deaths_country_sum.sum(axis=0)
deaths_country_sum= deaths_country_sum.reset_index()
deaths_country_sum.columns= ['Date', 'Number of deaths']
active_cases_country_sum= active_cases_country.drop(['Lat', 'Long'], axis=1)
active_cases_country_sum.set_index("country", inplace= True)
active_cases_country_sum= active_cases_country_sum.sum(axis=0)
active_cases_country_sum= active_cases_country_sum.reset_index()
active_cases_country_sum.columns= ['Date', 'Number of active cases']
recovered_cases_country_sum= recovered_cases_country.drop(['Lat', 'Long'], axis=1)
recovered_cases_country_sum.set_index("country", inplace= True)
recovered_cases_country_sum= recovered_cases_country_sum.sum(axis=0)
recovered_cases_country_sum= recovered_cases_country_sum.reset_index()
recovered_cases_country_sum.columns= ['Date', 'Number of recovered cases']
fig = make_subplots(rows=2, cols=2, subplot_titles=("<b>Confirmed Cases</b>", "<b>Deaths Reported</b>", "<b>Active cases</b>", "<b>Recovered Cases</b>"),
vertical_spacing=0.12)
# create trace1
trace1 = go.Scatter(
x = confirmed_cases_country_sum['Date'],
y = confirmed_cases_country_sum["Number of confirmed cases"],
name = "Confirmed Cases",
marker = dict(color = '#4D9DE0', opacity= 0.9),
mode= 'lines+markers', hoverinfo='x+y'
)
# create trace2
trace2 = go.Scatter(
x = deaths_country_sum['Date'],
y = deaths_country_sum["Number of deaths"],
name = "Deaths Reported",
marker = dict(color = '#E15554', opacity=0.9 ),
mode='lines+markers', hoverinfo= 'x+y')
trace3 = go.Scatter(
x = active_cases_country_sum['Date'],
y = active_cases_country_sum["Number of active cases"],
name = "Active Cases",
marker = dict(color = '#E6C74C', opacity=0.9),
mode='lines+markers', hoverinfo= 'x+y')
trace4 = go.Scatter(
x = recovered_cases_country_sum['Date'],
y = recovered_cases_country_sum["Number of recovered cases"],
name = "Recovered Cases",
marker = dict(color = '#3DB876', opacity= 1),
mode='lines+markers', hoverinfo= 'x+y')
fig.add_trace(trace1, row=1, col=1)
fig.add_trace(trace2, row=1, col=2)
fig.add_trace(trace3, row=2, col=1)
fig.add_trace(trace4, row=2, col=2)
# Update xaxis properties
# fig.update_xaxes(title_text="Country", row=1, col=1)
# fig.update_xaxes(title_text="Country", row=1, col=2)
fig.update_xaxes(title_text="<b>Date</b>", row=2, col=1, tickangle= -45)
fig.update_xaxes(title_text="<b>Date</b>", row=2, col=2, tickangle= -45)
fig.update_xaxes(tickangle= -45, row=1, col=1)
fig.update_xaxes(tickangle= -45, row=1, col=2)
# Update yaxis properties
fig.update_yaxes(title_text="<b>Number of cases</b>", row=1, col=1)
fig.update_yaxes(title_text="", row=1, col=2)
fig.update_yaxes(title_text="<b>Number of cases</b>", row=2, col=1)
fig.update_yaxes(title_text="", row=2, col=2)
# data = [trace1, trace2, trace3, trace4]
fig.update_layout(width=1200, height= 1000, title_text= '<b>Global COVID-19 trend</b>',
title_x=0.45, title_y= 0.97, plot_bgcolor='rgba(240,240,240,0.8)')
# fig = go.Figure(data = data, layout = layout)
iplot(fig)
confirmed_cases_continent_sorted= confirmed_cases_continent.sort_values(by= last_updated_date, ascending= False).iloc[:,[0,-1]]
recovered_cases_continent_sorted= recovered_cases_continent.sort_values(by= last_updated_date, ascending= False).iloc[:, [0,-1]]
deaths_continent_sorted= deaths_continent.sort_values(by= last_updated_date, ascending= False).iloc[:, [0,-1]]
active_cases_continent_sorted= active_cases_continent.sort_values(by= last_updated_date, ascending= False).iloc[:,[0,-1]]
fig = make_subplots(rows=2, cols=2, subplot_titles=("<b>Confirmed Cases</b>", "<b>Deaths Reported</b>", "<b>Active Cases</b>", "<b>Recovered Cases</b>"),
vertical_spacing=0.13)
# create trace1
trace1 = go.Bar(
x = confirmed_cases_continent_sorted['continent'],
y = confirmed_cases_continent_sorted[last_updated_date],
name = "Confirmed Cases",
marker = dict(color = 'rgba(80, 26, 80, 0.3)', opacity= 1,
line=dict(color='rgb(0,0,0)',width=1.5)),
text = confirmed_cases_continent_sorted[last_updated_date],
textposition='outside', hoverinfo= 'x+y')
# create trace2
trace2 = go.Bar(
x = deaths_continent_sorted['continent'],
y = deaths_continent_sorted[last_updated_date],
name = "Deaths Reported",
marker = dict(color = 'rgba(70, 174, 160, 0.3)', opacity=1,
line=dict(color='rgb(0,0,0)',width=1.5)),
text = deaths_continent_sorted[last_updated_date],
textposition='outside', hoverinfo= 'x+y')
trace3 = go.Bar(
x = active_cases_continent_sorted['continent'],
y = active_cases_continent_sorted[last_updated_date],
name = "Active Cases",
marker = dict(color = 'rgba(130, 109, 186, 0.3)', opacity= 1,
line=dict(color='rgb(0,0,0)',width=1.5)),
text = active_cases_continent_sorted[last_updated_date],
textposition='outside', hoverinfo= 'x+y')
trace4 = go.Bar(
x = recovered_cases_continent_sorted['continent'],
y = recovered_cases_continent_sorted[last_updated_date],
name = "Recovered Cases",
marker = dict(color = '#46cdcf', opacity= 0.5,
line=dict(color='rgb(0,0,0)',width=1.5)),
text = recovered_cases_continent_sorted[last_updated_date],
textposition='outside', hoverinfo= 'x+y')
fig.add_trace(trace1, row=1, col=1)
fig.add_trace(trace2, row=1, col=2)
fig.add_trace(trace3, row=2, col=1)
fig.add_trace(trace4, row=2, col=2)
# Update xaxis properties
# fig.update_xaxes(title_text="Continent", row=1, col=1)
# fig.update_xaxes(title_text="Continent", row=1, col=2)
fig.update_xaxes(title_text="", row=2, col=1)
fig.update_xaxes(title_text="", row=2, col=2)
# Update yaxis properties
fig.update_yaxes(title_text="<b>Number of cases</b>", row=1, col=1)
# fig.update_yaxes(title_text="Number of cases", row=1, col=2)
fig.update_yaxes(title_text="<b>Number of cases</b>", row=2, col=1)
# fig.update_yaxes(title_text="Number of cases", row=2, col=2)
# data = [trace1, trace2, trace3, trace4]
fig.update_layout(barmode = "group", width=1100, height= 1000, title_text= '<b>Continent-wise distribution</b>',
title_x=0.45, title_y= 0.97, plot_bgcolor='rgba(240,240,240,0.8)')
# fig = go.Figure(data = data, layout = layout)
iplot(fig)
n_rows, n_cols= 3,3
fig = make_subplots(rows=n_rows, cols=n_cols, subplot_titles=(confirmed_cases_continent_sorted["continent"].tolist()),
vertical_spacing=0.1,
specs=[[{"type": "pie"}]*n_cols]*n_rows)
cnt= 1
for row in range(n_rows):
for col in range(n_cols):
if (cnt>confirmed_cases_continent_sorted.shape[0]): break
continent_name= confirmed_cases_continent_sorted.iloc[row*(n_cols)+col,:]["continent"]
# print(continent_name, row*(n_cols)+col)
n_confirmed= confirmed_cases_continent_sorted[confirmed_cases_continent_sorted["continent"]==continent_name][last_updated_date].values
n_recovered= recovered_cases_continent_sorted[recovered_cases_continent_sorted["continent"]==continent_name][last_updated_date].values
n_deaths= deaths_continent_sorted[deaths_continent_sorted["continent"]==continent_name][last_updated_date].values
n_active= active_cases_continent_sorted[active_cases_continent_sorted["continent"]==continent_name][last_updated_date].values
continent_dist_df= pd.DataFrame({'Confirmed Cases': n_confirmed, 'Recovered Cases':n_recovered, \
'Deaths Reported': n_deaths, "Active Cases": n_active})
continent_dist_df_transpose= continent_dist_df.transpose().reset_index()
continent_dist_df_transpose.columns= ["Case type", "Number of cases"]
trace= go.Pie(
values= continent_dist_df_transpose.iloc[1:,:]["Number of cases"],
labels= continent_dist_df_transpose.iloc[1:,:]['Case type'],
name= continent_name,
# domain=dict(x=[0.5, 1.0], y= [0.2, 0.8]),
textinfo='percent', textfont_size=10,
hoverinfo= "label+value",
hole= .4, opacity= 0.8,
marker_colors=['#3DB876', '#E15554','#E6C74C'],
# marker = dict(line=dict(color='rgb(0,0,0)',width=1.5)),
textposition='outside')
fig.add_trace(trace, row= row+1, col= col+1)
cnt+=1
fig.update_layout(width=1200, height= 1000, title_text= '<b>Continent-wise distribution</b>',
title_x=0.45, title_y= 0.97, plot_bgcolor='rgba(240,240,240,0.8)')
iplot(fig)
table_stat_country_death_sorted= table_stat_country.sort_values(by="Total deaths", ascending= False)
fig = make_subplots(rows=1, cols=2, subplot_titles= ("<b>Confirmed Cases", "<b>Deaths Reported</b>"),
horizontal_spacing=0.1, specs=[[{"type": "domain"},{"type": "domain"}]])
trace1= go.Pie(
values= table_stat_country.iloc[1:21,:]["Total cases"],
labels= table_stat_country.iloc[1:21,:]['Country'],
name= "",
domain=dict(x=[0.0, 0.5], y= [0.2, 0.8]),
textinfo='percent', textfont_size=10,
hoverinfo= "label+value",
hole= .4, opacity= 0.9,
# marker_colors=['rgba(0,175,0,0.7)', 'rgba(255,0,0,0.8)','rgba(0,0,255,0.7)'],
# marker = dict(line=dict(color='rgb(0,0,0)',width=1.5)),
textposition='outside')
trace2= go.Pie(
values= table_stat_country_death_sorted.iloc[1:21,:]["Total deaths"],
labels= table_stat_country_death_sorted.iloc[1:21,:]['Country'],
name= "",
domain=dict(x=[0.5, 1.0], y= [0.2, 0.8]),
textinfo='percent', textfont_size=10,
hoverinfo= "label+value",
hole= .4, opacity= 0.9,
# marker_colors=['rgba(0,175,0,0.7)', 'rgba(255,0,0,0.8)','rgba(0,0,255,0.7)'],
# marker = dict(line=dict(color='rgb(0,0,0)',width=1.5)),
textposition='outside')
fig.add_trace(trace1, row=1, col=1)
fig.add_trace(trace2, row=1, col=2)
# fig.update_yaxes(title_text="Number of cases", row=1, col=1)
# fig.update_traces(marker_color='#46cdcf', opacity=0.8, textposition='outside')
fig.update_layout(width=1100, height= 500, title_text= '<b>Distribution among top 20 countries</b>',
title_x=0.45, title_y= 0.97, plot_bgcolor='rgba(240,240,240,0.8)')
iplot(fig)
confirmed_cases_country_top_10= confirmed_cases_country.sort_values(by= last_updated_date, ascending= False).iloc[:10,[0,-1]]
recovered_cases_country_top_10= recovered_cases_country[recovered_cases_country['country'].isin(confirmed_cases_country_top_10['country'])]
deaths_country_top_10= deaths_country[deaths_country['country'].isin(confirmed_cases_country_top_10['country'])]
active_cases_country_top_10= active_cases_country[active_cases_country['country'].isin(confirmed_cases_country_top_10['country'])]
recovered_cases_country_top_10= recovered_cases_country_top_10.sort_values(by= last_updated_date, ascending= False).iloc[:, [0,-1]]
deaths_country_top_10= deaths_country_top_10.sort_values(by= last_updated_date, ascending= False).iloc[:, [0,-1]]
active_cases_country_top_10= active_cases_country_top_10.sort_values(by= last_updated_date, ascending= False).iloc[:,[0,-1]]
fig = make_subplots(rows=2, cols=2, subplot_titles=("<b>Confirmed Cases</b>", "<b>Deaths Reported</b>", "<b>Active Cases</b>", "<b>Recovered Cases</b>"),
vertical_spacing=0.13)
# create trace1
trace1 = go.Bar(
x = confirmed_cases_country_top_10['country'],
y = confirmed_cases_country_top_10[last_updated_date],
name = "Confirmed cases",
marker = dict(color = 'rgba(80, 26, 80, 0.3)', opacity= 0.7,
line=dict(color='rgb(0,0,0)',width=1.5)),
text = confirmed_cases_country_top_10[last_updated_date],
textposition='outside', hoverinfo= 'x+y')
# create trace2
trace4 = go.Bar(
x = recovered_cases_country_top_10['country'],
y = recovered_cases_country_top_10[last_updated_date],
name = "Recovered cases",
marker = dict(color = '#46cdcf', opacity= 0.5,
line=dict(color='rgb(0,0,0)',width=1.5)),
text = recovered_cases_country_top_10[last_updated_date],
textposition='outside', hoverinfo= 'x+y')
trace2 = go.Bar(
x = deaths_country_top_10['country'],
y = deaths_country_top_10[last_updated_date],
name = "Deaths Reported",
marker = dict(color = 'rgba(70, 174, 160, 0.3)',
line=dict(color='rgb(0,0,0)',width=1.5)),
text = deaths_country_top_10[last_updated_date],
textposition='outside', hoverinfo= 'x+y')
trace3 = go.Bar(
x = active_cases_country_top_10['country'],
y = active_cases_country_top_10[last_updated_date],
name = "Active Cases",
marker = dict(color = 'rgba(130, 109, 186, 0.3)', opacity= 0.7,
line=dict(color='rgb(0,0,0)',width=1.5)),
text = active_cases_country_top_10[last_updated_date],
textposition='outside', hoverinfo= 'x+y')
fig.add_trace(trace1, row=1, col=1)
fig.add_trace(trace2, row=1, col=2)
fig.add_trace(trace3, row=2, col=1)
fig.add_trace(trace4, row=2, col=2)
# Update xaxis properties
# fig.update_xaxes(title_text="Country", row=1, col=1)
# fig.update_xaxes(title_text="Country", row=1, col=2)
fig.update_xaxes(title_text="", row=2, col=1)
fig.update_xaxes(title_text="", row=2, col=2)
# Update yaxis properties
fig.update_yaxes(title_text="<b>Number of cases</b>", row=1, col=1)
# fig.update_yaxes(title_text="Number of cases", row=1, col=2)
fig.update_yaxes(title_text="<b>Number of cases</b>", row=2, col=1)
# fig.update_yaxes(title_text="Number of cases", row=2, col=2)
# data = [trace1, trace2, trace3, trace4]
fig.update_layout(barmode = "group", width=1100, height= 1000, title_text= '<b>Distribution among top 10 countries</b>',
title_x=0.45, title_y= 0.97, plot_bgcolor='rgba(240,240,240,0.8)')
# fig = go.Figure(data = data, layout = layout)
iplot(fig)
n_rows, n_cols= 3,4
fig = make_subplots(rows=n_rows, cols=n_cols, subplot_titles=(confirmed_cases_country_top_10["country"].tolist()),
vertical_spacing=0.07, horizontal_spacing=0.1,
specs=[[{"type": "pie"}]*n_cols]*n_rows)
cnt= 1
for row in range(n_rows):
for col in range(n_cols):
if (cnt>confirmed_cases_country_top_10.shape[0]): break
country_name= confirmed_cases_country_top_10.iloc[row*(n_cols)+col,:]["country"]
# print(country_name, row*(n_cols)+col)
n_confirmed= confirmed_cases_country_top_10[confirmed_cases_country_top_10["country"]==country_name][last_updated_date].values
n_recovered= recovered_cases_country_top_10[recovered_cases_country_top_10["country"]==country_name][last_updated_date].values
n_deaths= deaths_country_top_10[deaths_country_top_10["country"]==country_name][last_updated_date].values
n_active= active_cases_country_top_10[active_cases_country_top_10["country"]==country_name][last_updated_date].values
country_dist_df= pd.DataFrame({'Confirmed cases': n_confirmed, 'Recovered cases':n_recovered, \
'Deaths Reported': n_deaths, "Active Cases": n_active})
country_dist_df_transpose= country_dist_df.transpose().reset_index()
country_dist_df_transpose.columns= ["Case type", "Number of cases"]
# country_dist_df_transpose= country_dist_df_transpose.iloc[1:,:] #COMMENT THIS LINE IF YOU WANT CONFIRMED CASES IN PIE CHART TOO
trace= go.Pie(
values= country_dist_df_transpose.iloc[1:,:]["Number of cases"],
labels= country_dist_df_transpose.iloc[1:,:]['Case type'],
name= 'Total confirmed cases: {}'.format(country_dist_df_transpose.iloc[0,1]),
# domain=dict(x=[0.5, 1.0], y= [0.2, 0.8]),
textinfo='percent', textfont_size=10,
hoverinfo= "label+value",
hole= .4, opacity= 0.8, marker_colors=['#3DB876', '#E15554','#E6C74C' ],
# marker = dict(line=dict(color='rgb(0,0,0)',width=1.5)),
textposition='outside')
fig.add_trace(trace, row= row+1, col= col+1)
cnt+=1
fig.update_layout(width=1100, height= 900, title_text= '<b>Distribution of top 10 countries</b>',
title_x=0.45, title_y= 0.97, plot_bgcolor='rgba(240,240,240,0.8)')
iplot(fig)
# df= pd.read_csv("https://raw.githubusercontent.com/emmagrimaldi/Capstone_Predicting_Storm_Damages/master/all_storms.csv")
# countries= np.unique(df.EVENT_TYPE)[:7]
n_countries= confirmed_cases_country.shape[0]
mapbox_access_token = os.environ.get('MAPBOX_ACCESS_TOKEN')
data = []
#preparing data for the scattermap plot
for i in range(confirmed_cases_country_sorted.shape[0]):
country_name= confirmed_cases_country_sorted.iloc[i,:]["country"]
confirmed_cases_country_df= confirmed_cases_country_sorted.loc[confirmed_cases_country_sorted['country'] == country_name,:]
deaths_country_df= deaths_country.loc[deaths_country['country'] == country_name,:]
active_cases_country_df= active_cases_country.loc[active_cases_country['country'] == country_name,:]
recovered_cases_country_df= recovered_cases_country.loc[recovered_cases_country['country'] == country_name,:]
country_data = dict(
lat = confirmed_cases_country_df['Lat'].values,
lon = confirmed_cases_country_df['Long'].values,
text = '<b>'+ confirmed_cases_country_df['country'].values[0] + \
':<br><br><b>Confirmed: '+str(confirmed_cases_country_df[last_updated_date].values[0])+ \
' (+' + str (confirmed_cases_country_df[last_updated_date].values[0] - confirmed_cases_country_df[second_last_updated_date].values[0]) +')' + \
'<br>Deaths: ' +str(deaths_country_df[last_updated_date].values[0])+ \
' (+' + str (deaths_country_df[last_updated_date].values[0] - deaths_country_df[second_last_updated_date].values[0]) +')' + \
'<br>Active: ' +str(active_cases_country_df[last_updated_date].values[0])+ \
'<br>Recovered: '+str(recovered_cases_country_df[last_updated_date].values[0])+'</b>',
hoverinfo= "text",
name = country_name,
marker = dict(
#170 is the bubble size for topmost country and rest of the countries are scaled as per their confirmed cases
size = (170*confirmed_cases_country_df[last_updated_date])/confirmed_cases_country_sorted.iloc[0,:][last_updated_date],
symbol= 'circle', sizemin= 2,
color = "rgba(252, 146, 114, 0.8)",
# color= color_map[continent_name],
# line=dict(
# color='rgba(255,255,255,0.8)',
# width=1.5)
),
type = 'scattermapbox'
)
data.append(country_data)
#basic layout info for the plot
layout = dict(
height = 800,
# top, bottom, left and right margins
margin = dict(t = 0, b = 0, l = 0, r = 0),
font = dict(color = '#FFFFFF', size = 11),
paper_bgcolor = '#000000',
showlegend=False,
mapbox = dict(
# here you need the token from Mapbox
accesstoken = mapbox_access_token,
bearing = 0,
# where we want the map to be centered
center = dict(
lat = 43,
lon = 12
),
# we want the map to be "parallel" to our screen, with no angle
pitch = 0,
# default level of zoom
zoom = 1,
# default map style
style = 'dark'
)
)
# assigning the annotations to the layout
# layout['annotations'] = annotations
#designing dropdown menu for the layout of plot
list_updatemenus=list()
list_updatemenus_1_buttons= []
#adding button for World (all countries at once in map)
country_name= "All"
visible = [True] * n_countries
temp_dict = dict(label = country_name,
method = 'update',
args = [{'visible': visible},
{'title': 'Country: {}'.format(country_name)}])
list_updatemenus_1_buttons.append(temp_dict)
#adding buttons for all countries individually
for i in range(1,table_stat_country.shape[0]):
country_name= table_stat_country.iloc[i,0]
lat= confirmed_cases_country_sorted.loc[confirmed_cases_country_sorted["country"]==country_name, "Lat"].values[0]
long= confirmed_cases_country_sorted.loc[confirmed_cases_country_sorted["country"]==country_name, "Long"].values[0]
visible = [False] * n_countries
visible[i-1] = True
temp_dict = dict(label = country_name,
method = 'update',
args = [{'visible': visible},
{layout['mapbox']['center']['lat']: lat, layout['mapbox']['center']['lon']: long},
{'title': 'Country: {}'.format(country_name)}])
list_updatemenus_1_buttons.append(temp_dict)
# drop-down 2: select type of storm event to visualize
list_updatemenus_1 = dict(
# for each button I specify which dictionaries of my data list I want to visualize. Remember I have 7 different
# types of storms but I have 8 options: the first will show all of them, while from the second to the last option, only
# one type at the time will be shown on the map
buttons= list_updatemenus_1_buttons,
type= 'dropdown',
# direction where the drop-down expands when opened
direction = 'down',
# positional arguments
x = 0.01,
xanchor = 'left',
y = 0.99,
yanchor = 'bottom',
# fonts and border
bgcolor = '#EEEEEE',
bordercolor = '#FFFFFF',
font = dict(color = '#000000', size=14)
)
list_updatemenus.append(list_updatemenus_1)
# assign the list of dictionaries to the layout dictionary
layout['updatemenus'] = list_updatemenus
figure = dict(data = data, layout = layout)
iplot(figure)
n_countries= confirmed_cases_country.shape[0]
data = []
#Preparing data for World scatter plot
confirmed_cases_world= confirmed_cases_country.iloc[:,3:].sum(axis=0)
confirmed_cases_world_time= confirmed_cases_world.transpose().reset_index()
confirmed_cases_world_time.columns= ['Date', 'Number of confirmed cases']
country_trace = go.Scatter(
x = confirmed_cases_world_time['Date'],
y = confirmed_cases_world_time["Number of confirmed cases"],
name = "World",
# marker = dict(color = '#46cdcf', opacity= 0.7, line=dict(color='rgb(0,0,0)',width=1.5)),
# text= '+'+ str (new_confirmed_cases_country_time["Number of new confirmed cases"]),
text= "World"+ '<br><br>',
# textposition='outside',
mode= 'lines+markers', hoverinfo= 'text+x+y'
)
data.append(country_trace)
#preparing data for the scatter plot country-wise
for i in range(confirmed_cases_country_sorted.shape[0]):
country_name= confirmed_cases_country_sorted.iloc[i,:]["country"]
confirmed_cases_country_df= confirmed_cases_country_sorted.loc[confirmed_cases_country_sorted['country'] == country_name,:]
confirmed_cases_country_time= confirmed_cases_country_df.transpose().reset_index().iloc[3:,:]
confirmed_cases_country_time.columns= ['Date', 'Number of confirmed cases']
# deaths_country_df= deaths_country.loc[deaths_country['country'] == country_name,:]
# active_cases_country_df= active_cases_country.loc[active_cases_country['country'] == country_name,:]
# recovered_cases_country_df= recovered_cases_country.loc[recovered_cases_country['country'] == country_name,:]
country_trace = go.Scatter(
x = confirmed_cases_country_time['Date'],
y = confirmed_cases_country_time["Number of confirmed cases"],
name = country_name,
text= country_name+ '<br><br>',
# marker = dict(color = 'rgba(80, 26, 80, 0.8)', opacity= 1),
mode= 'lines+markers', hoverinfo='text+x+y'
)
data.append(country_trace)
# assigning the annotations to the layout
# layout['annotations'] = annotations
#designing dropdown menu for the layout of plot
list_updatemenus_buttons= []
title_text= '<b>Confirmed Cases Trend</b>'
default_dict= dict(label = "All",
method = 'update',
args = [{'visible': [True]*(n_countries+1)},
{'title': title_text}])
list_updatemenus_buttons.append(default_dict)
title_text= '<b>Total confirmed cases: {}<br>Confirmed cases per million: {}</b>' \
.format(table_stat_country.loc[table_stat_country["Country"]=="World", "Total cases"].values[0], \
table_stat_country.loc[table_stat_country["Country"]=="World", "Total cases per million"].values[0])
visible= [False]*(n_countries+1)
visible[0]= True
default_dict= dict(label = "World",
method = 'update',
args = [{'visible': visible},
{'title': title_text}])
list_updatemenus_buttons.append(default_dict)
for i in range(confirmed_cases_country_sorted.shape[0]):
country_name= confirmed_cases_country_sorted.iloc[i,0]
title_text= '<b>Total confirmed cases: {}<br>Confirmed cases per million: {}</b>' \
.format(table_stat_country.loc[table_stat_country["Country"]==country_name, "Total cases"].values[0], \
table_stat_country.loc[table_stat_country["Country"]==country_name, "Total cases per million"].values[0])
visible = [False] * (n_countries+1)
visible[i+1] = True
temp_dict = dict(label = country_name,
method = 'update',
args = [{'visible': visible},
{'title': title_text}, {'font':11}])
list_updatemenus_buttons.append(temp_dict)
# drop-down 2: select type of storm event to visualize
list_updatemenus = list( [dict(
# for each button I specify which dictionaries of my data list I want to visualize. Remember I have 7 different
# types of storms but I have 8 options: the first will show all of them, while from the second to the last option, only
# one type at the time will be shown on the map
buttons= list_updatemenus_buttons,
type= 'dropdown',
# direction where the drop-down expands when opened
direction = 'down',
# positional arguments
x = -0.07,
xanchor = 'right',
y = 0.90,
yanchor = 'bottom',
# fonts and border
bgcolor = '#EEEEEE',
bordercolor = '#FFFFFF',
font = dict(color = '#000000', size=14)
)
])
#basic layout info for the plot
layout = dict(
height = 600, width= 1200,
plot_bgcolor='rgba(240,240,240,0.8)',
title = '<b>Trend of confirmed cases</b>', title_x=0.55, title_y= 0.9,
xaxis= dict(title= '<b>Date</b>', tickangle= -45), yaxis= dict(title= ''),
# top, bottom, left and right margins
# margin = dict(t = 0, b = 0, l = 0, r = 0),
font = dict(color = 'rgba(0, 0, 0, 0.8)'),
# paper_bgcolor = '#000000',
# showlegend=False,
)
# list_updatemenus.append(list_updatemenus_1)
# assign the list of dictionaries to the layout dictionary
layout['updatemenus'] = list_updatemenus
figure = dict(data = data, layout = layout)
iplot(figure)
data = []
n_countries= confirmed_cases_country.shape[0]
new_confirmed_cases_country= confirmed_cases_country_sorted.copy()
for i in range(4, confirmed_cases_country_sorted.shape[1]):
new_confirmed_cases_country.iloc[:,i]= confirmed_cases_country_sorted.iloc[:,i]-confirmed_cases_country_sorted.iloc[:,i-1]
#find number of days it is taking to get doubled for each country
#adding trace for World data for default dropdown selection
new_confirmed_cases_world= new_confirmed_cases_country.iloc[:,3:].sum(axis=0)
new_confirmed_cases_country_time= new_confirmed_cases_world.transpose().reset_index().iloc[3:,:]
new_confirmed_cases_country_time.columns= ['Date', 'Number of new confirmed cases']
country_trace = go.Scatter(
x = new_confirmed_cases_country_time['Date'],
y = new_confirmed_cases_country_time["Number of new confirmed cases"],
name = "World",
# marker = dict(color = '#46cdcf', opacity= 0.7, line=dict(color='rgb(0,0,0)',width=1.5)),
# text= '+'+ str (new_confirmed_cases_country_time["Number of new confirmed cases"]),
text= "World"+ '<br><br>',
# textposition='outside',
mode= 'lines+markers', hoverinfo= 'text+x+y'
)
data.append(country_trace)
#preparing data for the scatter plot country-wise
for i in range(confirmed_cases_country_sorted.shape[0]):
country_name= confirmed_cases_country_sorted.iloc[i,:]["country"]
new_confirmed_cases_country_df= new_confirmed_cases_country.loc[new_confirmed_cases_country['country'] == country_name,:]
new_confirmed_cases_country_time= new_confirmed_cases_country_df.transpose().reset_index().iloc[3:,:]
new_confirmed_cases_country_time.columns= ['Date', 'Number of new confirmed cases']
# deaths_country_df= deaths_country.loc[deaths_country['country'] == country_name,:]
# active_cases_country_df= active_cases_country.loc[active_cases_country['country'] == country_name,:]
# recovered_cases_country_df= recovered_cases_country.loc[recovered_cases_country['country'] == country_name,:]
country_trace = go.Scatter(
x = new_confirmed_cases_country_time['Date'],
y = new_confirmed_cases_country_time["Number of new confirmed cases"],
name = country_name,
# marker = dict(color = '#46cdcf', opacity= 0.7, line=dict(color='rgb(0,0,0)',width=1.5)),
# text= '+'+ str (new_confirmed_cases_country_time["Number of new confirmed cases"]),
# textposition='outside',
text= country_name+ '<br><br>',
mode= "lines+markers", hoverinfo= 'text+x+y'
)
data.append(country_trace)
#designing dropdown menu for the layout of plot
list_updatemenus_buttons= []
title_text= '<b>Trend of new confirmed cases</b>'
visible = [True] * (n_countries+1)
default_dict= dict(label = "All",
method = 'update',
args = [{'visible': visible},
{'title': title_text}])
list_updatemenus_buttons.append(default_dict)
visible = [False] * (n_countries+1)
visible[0]= True
default_dict= dict(label = "World",
method = 'update',
args = [{'visible': visible},
{'title': title_text}])
list_updatemenus_buttons.append(default_dict)
for i in range(confirmed_cases_country_sorted.shape[0]):
country_name= confirmed_cases_country_sorted.iloc[i,0]
title_text= '<b>Trend of new confirmed cases</b>'
visible = [False] * (n_countries+1)
visible[i+1] = True
temp_dict = dict(label = country_name,
method = 'update',
args = [{'visible': visible},
{'title': title_text}, {'font':11}])
list_updatemenus_buttons.append(temp_dict)
# drop-down 2: select type of storm event to visualize
list_updatemenus = list( [dict(
# for each button I specify which dictionaries of my data list I want to visualize. Remember I have 7 different
# types of storms but I have 8 options: the first will show all of them, while from the second to the last option, only
# one type at the time will be shown on the map
buttons= list_updatemenus_buttons,
type= 'dropdown',
# direction where the drop-down expands when opened
direction = 'down',
# positional arguments
x = -0.09,
xanchor = 'right',
y = 0.90,
yanchor = 'bottom',
# fonts and border
bgcolor = '#EEEEEE',
bordercolor = '#FFFFFF',
font = dict(color = '#000000', size=14)
)
])
#basic layout info for the plot
layout = dict(
height = 600, width= 1200,
plot_bgcolor='rgba(240,240,240,0.8)',
title = '<b>Trend of new confirmed cases</b>', title_x=0.55, title_y= 0.9,
xaxis= dict(title= '<b>Date</b>', tickangle= -45), yaxis= dict(title= ''),
# top, bottom, left and right margins
# margin = dict(t = 0, b = 0, l = 0, r = 0),
font = dict(color = 'rgba(0, 0, 0, 0.8)'),
# paper_bgcolor = '#000000',
# showlegend=False,
)
# list_updatemenus.append(list_updatemenus_1)
# assign the list of dictionaries to the layout dictionary
layout['updatemenus'] = list_updatemenus
figure = dict(data = data, layout = layout)
iplot(figure)
# df= pd.read_csv("https://raw.githubusercontent.com/emmagrimaldi/Capstone_Predicting_Storm_Damages/master/all_storms.csv")
# countries= np.unique(df.EVENT_TYPE)[:7]
n_countries= confirmed_cases_country.shape[0]
# confirmed_cases_country_sorted= confirmed_cases_country.sort_values(by= last_updated_date, ascending= False)
data = []
#adding trace for World data for default dropdown selection
country_name= table_stat_country.iloc[0,:]["Country"]
n_confirmed= table_stat_country[table_stat_country["Country"]==country_name]["Total cases"].values
n_recovered= table_stat_country[table_stat_country["Country"]==country_name]["Total recovered"].values
n_deaths= table_stat_country[table_stat_country["Country"]==country_name]["Total deaths"].values
n_active= table_stat_country[table_stat_country["Country"]==country_name]["Total active"].values
country_dist_df= pd.DataFrame({'Confirmed cases': n_confirmed, 'Recovered cases':n_recovered, \
'Deaths Reported': n_deaths, "Active Cases": n_active})
country_dist_df_transpose= country_dist_df.transpose().reset_index()
country_dist_df_transpose.columns= ["Case type", "Number of cases"]
country_trace= go.Pie(
values= country_dist_df_transpose.iloc[1:,:]["Number of cases"],
labels= country_dist_df_transpose.iloc[1:,:]['Case type'],
name= 'Total confirmed cases: {}'.format(country_dist_df_transpose.iloc[0,1]),
# domain=dict(x=[0.5, 1.0], y= [0.2, 0.8]),
textinfo='percent', textfont_size=10,
hoverinfo= "label+value",
hole= .4, opacity= 0.8,
marker_colors=['#3DB876', '#E15554','#E6C74C'],
textposition='outside')
data.append(country_trace)
#preparing data for the pie chart country-wise
for i in range(confirmed_cases_country_sorted.shape[0]):
country_name= confirmed_cases_country_sorted.iloc[i,:]["country"]
n_confirmed= confirmed_cases_country[confirmed_cases_country["country"]==country_name][last_updated_date].values
n_recovered= recovered_cases_country[recovered_cases_country["country"]==country_name][last_updated_date].values
n_deaths= deaths_country[deaths_country["country"]==country_name][last_updated_date].values
n_active= active_cases_country[active_cases_country["country"]==country_name][last_updated_date].values
country_dist_df= pd.DataFrame({'Confirmed cases': n_confirmed, 'Recovered cases':n_recovered, \
'Deaths Reported': n_deaths, "Active Cases": n_active})
country_dist_df_transpose= country_dist_df.transpose().reset_index()
country_dist_df_transpose.columns= ["Case type", "Number of cases"]
country_trace= go.Pie(
values= country_dist_df_transpose.iloc[1:,:]["Number of cases"],
labels= country_dist_df_transpose.iloc[1:,:]['Case type'],
name= 'Total confirmed cases: {}'.format(country_dist_df_transpose.iloc[0,1]),
# domain=dict(x=[0.5, 1.0], y= [0.2, 0.8]),
textinfo='percent', textfont_size=10,
hoverinfo= "label+value",
hole= .4, opacity= 0.8, visible=False,
marker_colors=['#3DB876', '#E15554','#E6C74C'],
textposition='outside')
data.append(country_trace)
#designing dropdown menu for the layout of plot
list_updatemenus_buttons= []
title_text= '<b>Total confirmed cases: {}</b>' \
.format(table_stat_country.loc[table_stat_country["Country"]=="World", "Total cases"].values[0])
visible = [False] * (n_countries+1)
visible[0]= True
default_dict= dict(label = "World",
method = 'update',
args = [{'visible': visible},
{'title': title_text}])
list_updatemenus_buttons.append(default_dict)
for i in range(confirmed_cases_country_sorted.shape[0]):
country_name= confirmed_cases_country_sorted.iloc[i,0]
title_text= '<b>Total confirmed cases: {}</b>' \
.format(table_stat_country.loc[table_stat_country["Country"]==country_name, "Total cases"].values[0])
visible = [False] * (n_countries+1)
visible[i+1] = True
temp_dict = dict(label = country_name,
method = 'update',
args = [{'visible': visible},
{'title': title_text}, {'font':11}])
list_updatemenus_buttons.append(temp_dict)
# drop-down 2: select type of storm event to visualize
list_updatemenus = list( [dict(
# for each button I specify which dictionaries of my data list I want to visualize. Remember I have 7 different
# types of storms but I have 8 options: the first will show all of them, while from the second to the last option, only
# one type at the time will be shown on the map
buttons= list_updatemenus_buttons,
type= 'dropdown', active= 0,
# direction where the drop-down expands when opened
direction = 'down',
# positional arguments
x = -0.07,
xanchor = 'right',
y = 0.90,
yanchor = 'bottom',
# fonts and border
bgcolor = '#EEEEEE',
bordercolor = '#FFFFFF',
font = dict(color = '#000000', size=14)
)
])
#basic layout info for the plot
layout = dict(
height = 600, width= 1000,
plot_bgcolor='rgba(240,240,240,0.8)',
title = '<b>Country-wise distribution</b>', title_x=0.55, title_y= 0.9,
xaxis= dict(title= 'Date', tickangle= -45), yaxis= dict(title= 'Number of deaths reported'),
# top, bottom, left and right margins
# margin = dict(t = 0, b = 0, l = 0, r = 0),
font = dict(color = 'rgba(0, 0, 0, 0.8)'),
# paper_bgcolor = '#000000',
# showlegend=False,
)
# list_updatemenus.append(list_updatemenus_1)
# assign the list of dictionaries to the layout dictionary
layout['updatemenus'] = list_updatemenus
figure = dict(data = data, layout = layout)
iplot(figure)
columns= confirmed_cases_country.columns
new_confirmed_cases_country_1= new_confirmed_cases_country.copy()
new_confirmed_cases_country_1["n_days_cases_double"]=0
for i in range(confirmed_cases_country_sorted.shape[0]):
country_name= confirmed_cases_country_sorted.iloc[i,:]["country"]
# print(country_name)
n_days=1
while(True):
try:
if(confirmed_cases_country.loc[confirmed_cases_country["country"]==country_name, last_updated_date].values[0]/confirmed_cases_country.loc[confirmed_cases_country["country"]==country_name, columns[-n_days-1]].values[0]<2):
n_days+=1
else:
new_confirmed_cases_country_1.loc[new_confirmed_cases_country_1["country"]==country_name,"n_days_cases_double"]= n_days
break
except:
new_confirmed_cases_country_1.loc[new_confirmed_cases_country_1["country"]==country_name,"n_days_cases_double"]= float("inf")
data= go.Bar(
y = new_confirmed_cases_country_1.iloc[:50,:]['country'],
x = new_confirmed_cases_country_1.iloc[:50,:]["n_days_cases_double"],
name = country_name,
marker = dict(color = '#46cdcf', opacity= 0.7,
line=dict(color='rgb(0,0,0)',width=1.5)),
text= new_confirmed_cases_country_1["n_days_cases_double"],
textposition='outside', hoverinfo= 'y+x',
orientation= 'h'
)
layout = dict(
height = 1200, width= 1000,
plot_bgcolor='rgba(240,240,240,0.8)',
title = '<b>Number of days to double - Confirmed cases</b>', title_x=0.55, title_y= 0.95,
xaxis= dict(title= '<b>Numbe of days</b>'), yaxis= dict(title= '', autorange="reversed"),
# top, bottom, left and right margins
# margin = dict(t = 0, b = 0, l = 0, r = 0),
font = dict(color = 'rgba(0, 0, 0, 0.8)'),
# paper_bgcolor = '#000000',
showlegend=False,
)
figure = dict(data = data, layout = layout)
iplot(figure)
n_countries= confirmed_cases_country.shape[0]
data = []
#Preparing data for World scatter plot
deaths_world= deaths_country.iloc[:,3:].sum(axis=0)
deaths_world_time= deaths_world.transpose().reset_index()
deaths_world_time.columns= ['Date', 'Number of deaths']
country_trace = go.Scatter(
x = deaths_world_time['Date'],
y = deaths_world_time["Number of deaths"],
name = "World",
# marker = dict(color = '#46cdcf', opacity= 0.7, line=dict(color='rgb(0,0,0)',width=1.5)),
# text= '+'+ str (new_confirmed_cases_country_time["Number of new confirmed cases"]),
text= "World"+ '<br><br>',
# textposition='outside',
mode= 'lines+markers', hoverinfo= 'text+x+y'
)
data.append(country_trace)
#preparing data for the scatter plot country-wise
for i in range(deaths_country_sorted.shape[0]):
country_name= deaths_country_sorted.iloc[i,:]["country"]
deaths_country_df= deaths_country.loc[deaths_country['country'] == country_name,:]
deaths_country_time= deaths_country_df.transpose().reset_index().iloc[3:,:]
deaths_country_time.columns= ['Date', 'Number of deaths']
# deaths_country_df= deaths_country.loc[deaths_country['country'] == country_name,:]
# active_cases_country_df= active_cases_country.loc[active_cases_country['country'] == country_name,:]
# recovered_cases_country_df= recovered_cases_country.loc[recovered_cases_country['country'] == country_name,:]
country_trace = go.Scatter(
x = deaths_country_time['Date'],
y = deaths_country_time["Number of deaths"],
name = country_name,
text= country_name+ '<br><br>',
# marker = dict(color = 'rgba(80, 26, 80, 0.8)', opacity= 1),
mode= 'lines+markers', hoverinfo='text+x+y'
)
data.append(country_trace)
#designing dropdown menu for the layout of plot
list_updatemenus_buttons= []
title_text= '<b>Deaths trend </b>'
default_dict= dict(label = "All",
method = 'update',
args = [{'visible': [True]*(n_countries+1)},
{'title': title_text}])
list_updatemenus_buttons.append(default_dict)
title_text= '<b>Total deaths reported: {}<br>Deaths per million: {}<b>' \
.format(table_stat_country.loc[table_stat_country["Country"]=="World", "Total deaths"].values[0], \
table_stat_country.loc[table_stat_country["Country"]=="World", "Total deaths per million"].values[0])
visible= [False]*(n_countries+1)
visible[0]= True
default_dict= dict(label = "World",
method = 'update',
args = [{'visible': visible},
{'title': title_text}])
list_updatemenus_buttons.append(default_dict)
for i in range(deaths_country_sorted.shape[0]):
country_name= deaths_country_sorted.iloc[i,0]
title_text= '<b>Total deaths reported: {}<br>Deaths per million: {}</b>' \
.format(table_stat_country.loc[table_stat_country["Country"]==country_name, "Total deaths"].values[0], \
table_stat_country.loc[table_stat_country["Country"]==country_name, "Total deaths per million"].values[0])
visible = [False] * (n_countries+1)
visible[i+1] = True
temp_dict = dict(label = country_name,
method = 'update',
args = [{'visible': visible},
{'title': title_text}, {'font':11}])
list_updatemenus_buttons.append(temp_dict)
# drop-down 2: select type of storm event to visualize
list_updatemenus = list( [dict(
# for each button I specify which dictionaries of my data list I want to visualize. Remember I have 7 different
# types of storms but I have 8 options: the first will show all of them, while from the second to the last option, only
# one type at the time will be shown on the map
buttons= list_updatemenus_buttons,
type= 'dropdown',
# direction where the drop-down expands when opened
direction = 'down',
# positional arguments
x = -0.07,
xanchor = 'right',
y = 0.90,
yanchor = 'bottom',
# fonts and border
bgcolor = '#EEEEEE',
bordercolor = '#FFFFFF',
font = dict(color = '#000000', size=14)
)
])
#basic layout info for the plot
layout = dict(
height = 600, width= 1200,
plot_bgcolor='rgba(240,240,240,0.8)',
title = '<b>Trend of deaths reported</b>', title_x=0.55, title_y= 0.9,
xaxis= dict(title= '<b>Date</b>', tickangle= -45), yaxis= dict(title= ''),
# top, bottom, left and right margins
# margin = dict(t = 0, b = 0, l = 0, r = 0),
font = dict(color = 'rgba(0, 0, 0, 0.8)'),
# paper_bgcolor = '#000000',
# showlegend=False,
)
# list_updatemenus.append(list_updatemenus_1)
# assign the list of dictionaries to the layout dictionary
layout['updatemenus'] = list_updatemenus
figure = dict(data = data, layout = layout)
iplot(figure)
data = []
n_countries= deaths_country.shape[0]
new_deaths_country= deaths_country_sorted.copy()
for i in range(4, deaths_country_sorted.shape[1]):
new_deaths_country.iloc[:,i]= deaths_country_sorted.iloc[:,i]-deaths_country_sorted.iloc[:,i-1]
#adding trace for World data for default dropdown selection
new_deaths_world= new_deaths_country.iloc[:,3:].sum(axis=0)
new_deaths_country_time= new_deaths_world.transpose().reset_index().iloc[3:,:]
new_deaths_country_time.columns= ['Date', 'Number of new deaths']
country_trace = go.Scatter(
x = new_deaths_country_time['Date'],
y = new_deaths_country_time["Number of new deaths"],
name = "World",
# marker = dict(color = '#46cdcf', opacity= 0.7,line=dict(color='rgb(0,0,0)',width=1.5)),
# text= '+'+ str (new_deaths_country_time["Number of new deaths"]),
# textposition='outside',
text= "World"+ '<br><br>',
mode= 'lines+markers', hoverinfo= 'text+x+y'
)
data.append(country_trace)
#preparing data for the scatter plot country-wise
for i in range(deaths_country_sorted.shape[0]):
country_name= deaths_country_sorted.iloc[i,:]["country"]
new_deaths_country_df= new_deaths_country.loc[new_deaths_country['country'] == country_name,:]
new_deaths_country_time= new_deaths_country_df.transpose().reset_index().iloc[3:,:]
new_deaths_country_time.columns= ['Date', 'Number of new deaths']
# deaths_country_df= deaths_country.loc[deaths_country['country'] == country_name,:]
# active_cases_country_df= active_cases_country.loc[active_cases_country['country'] == country_name,:]
# recovered_cases_country_df= recovered_cases_country.loc[recovered_cases_country['country'] == country_name,:]
country_trace = go.Scatter(
x = new_deaths_country_time['Date'],
y = new_deaths_country_time["Number of new deaths"],
name = country_name,
# marker = dict(color = '#46cdcf', opacity= 0.7, line=dict(color='rgb(0,0,0)',width=1.5)),
# text= '+'+ str (new_deaths_country_time["Number of new deaths"]),
# textposition='outside',
text= country_name+ '<br><br>',
mode= 'lines+markers', hoverinfo= 'text+x+y'
)
data.append(country_trace)
#designing dropdown menu for the layout of plot
list_updatemenus_buttons= []
title_text= '<b>Trend of new deaths reported </b>'
visible = [True] * (n_countries+1)
default_dict= dict(label = "All",
method = 'update',
args = [{'visible': visible},
{'title': title_text}])
list_updatemenus_buttons.append(default_dict)
visible = [False] * (n_countries+1)
visible[0]= True
default_dict= dict(label = "World",
method = 'update',
args = [{'visible': visible},
{'title': title_text}])
list_updatemenus_buttons.append(default_dict)
for i in range(deaths_country_sorted.shape[0]):
country_name= deaths_country_sorted.iloc[i,:]["country"]
title_text= '<b>Trend of new deaths reported</b>'
visible = [False] * (n_countries+1)
visible[i+1] = True
temp_dict = dict(label = country_name,
method = 'update',
args = [{'visible': visible},
{'title': title_text}, {'font':11}])
list_updatemenus_buttons.append(temp_dict)
# drop-down 2: select type of storm event to visualize
list_updatemenus = list( [dict(
# for each button I specify which dictionaries of my data list I want to visualize. Remember I have 7 different
# types of storms but I have 8 options: the first will show all of them, while from the second to the last option, only
# one type at the time will be shown on the map
buttons= list_updatemenus_buttons,
type= 'dropdown',
# direction where the drop-down expands when opened
direction = 'down',
# positional arguments
x = -0.09,
xanchor = 'right',
y = 0.90,
yanchor = 'bottom',
# fonts and border
bgcolor = '#EEEEEE',
bordercolor = '#FFFFFF',
font = dict(color = '#000000', size=14)
)
])
#basic layout info for the plot
layout = dict(
height = 600, width= 1200,
plot_bgcolor='rgba(240,240,240,0.8)',
title = '<b>Trend of new deaths reported</b>', title_x=0.55, title_y= 0.9,
xaxis= dict(title= '<b>Date</b>', tickangle= -45), yaxis= dict(title= ''),
# top, bottom, left and right margins
# margin = dict(t = 0, b = 0, l = 0, r = 0),
font = dict(color = 'rgba(0, 0, 0, 0.8)'),
# paper_bgcolor = '#000000',
# showlegend=False,
)
# list_updatemenus.append(list_updatemenus_1)
# assign the list of dictionaries to the layout dictionary
layout['updatemenus'] = list_updatemenus
figure = dict(data = data, layout = layout)
iplot(figure)
columns= deaths_country.columns
new_deaths_country_1= new_deaths_country.copy()
new_deaths_country_1["n_days_deaths_double"]=0
for i in range(deaths_country_sorted.shape[0]):
country_name= deaths_country_sorted.iloc[i,:]["country"]
# print(country_name)
n_days=1
while(True):
try:
if(deaths_country.loc[deaths_country["country"]==country_name, last_updated_date].values[0]/deaths_country.loc[deaths_country["country"]==country_name, columns[-n_days-1]].values[0]<2):
n_days+=1
else:
new_deaths_country_1.loc[new_deaths_country_1["country"]==country_name,"n_days_deaths_double"]= n_days
break
except:
new_deaths_country_1.loc[new_deaths_country_1["country"]==country_name,"n_days_deaths_double"]= float("inf")
data= go.Bar(
y = new_deaths_country_1.iloc[:50,:]['country'],
x = new_deaths_country_1.iloc[:50,:]["n_days_deaths_double"],
name = country_name,
marker = dict(color = '#46cdcf', opacity= 0.7,
line=dict(color='rgb(0,0,0)',width=1.5)),
text= new_deaths_country_1["n_days_deaths_double"],
textposition='outside', hoverinfo= 'x+y',
orientation= 'h'
)
layout = dict(
height = 1200, width= 1000,
plot_bgcolor='rgba(240,240,240,0.8)',
title = '<b>Number of days to double - Deaths reported</b>', title_x=0.55, title_y= 0.95,
xaxis= dict(title= '<b>Numbe of days</b>'), yaxis= dict(title= '', autorange="reversed"),
# top, bottom, left and right margins
# margin = dict(t = 0, b = 0, l = 0, r = 0),
font = dict(color = 'rgba(0, 0, 0, 0.8)'),
# paper_bgcolor = '#000000',
showlegend=False,
)
figure = dict(data = data, layout = layout)
iplot(figure)
data = []
#preparing data for the scatter plot country-wise
for i in range(country_list_test.shape[0]):
country_name= country_list_test[i]
test_df_grp_country= test_df_grp.loc[test_df_grp['country'] == country_name,:]
# test_df_grp_country= test_df_grp_country.dropna(how="any", subset=["Cumulative total"])
# test_df_
# deaths_country_time= deaths_country_df.transpose().reset_index().iloc[3:,:]
# deaths_country_time.columns= ['Date', 'Number of deaths']
# deaths_country_df= deaths_country.loc[deaths_country['country'] == country_name,:]
# active_cases_country_df= active_cases_country.loc[active_cases_country['country'] == country_name,:]
# recovered_cases_country_df= recovered_cases_country.loc[recovered_cases_country['country'] == country_name,:]
country_trace = go.Scatter(
x = test_df_grp_country['Date'],
y = test_df_grp_country["Total tests"],
name = country_name,
text= country_name+ '<br><br>',
# marker = dict(color = 'rgba(80, 26, 80, 0.8)', opacity= 1),
mode= 'lines+markers', hoverinfo= "text+x+y"
)
data.append(country_trace)
#annotaions for the layout of plot
annotations = [dict(
# text I want to display. I used <br> to break it into two lines
text = 'All US storm events that caused more than $50k of economic damage,<br> from 2000 until today',
# font and border characteristics
font = dict(color = '#FFFFFF', size = 14), borderpad = 10,
# positional arguments
x = 0.05, y = 0.05, xref = 'paper', yref = 'paper', align = 'left',
# don't show arrow and set background color
showarrow = False, bgcolor = 'black'
)]
# assigning the annotations to the layout
# layout['annotations'] = annotations
#designing dropdown menu for the layout of plot
list_updatemenus_buttons= []
title_text= '<b>Trend of number of tests performed</b>'
default_dict= dict(label = "All",
method = 'update',
args = [{'visible': [True]*n_countries_test},
{'title': title_text}])
list_updatemenus_buttons.append(default_dict)
for i in range(country_list_test.shape[0]):
country_name= country_list_test[i]
title_text= '<b>Tests per million: {}</b>' \
.format(test_df_grp.loc[test_df_grp["country"]==country_name, "Total tests per million"].iloc[-1])
visible = [False] * n_countries_test
visible[i] = True
temp_dict = dict(label = country_name,
method = 'update',
args = [{'visible': visible},
{'title': title_text}, {'font':11}])
list_updatemenus_buttons.append(temp_dict)
# drop-down 2: select type of storm event to visualize
list_updatemenus = list( [dict(
# for each button I specify which dictionaries of my data list I want to visualize. Remember I have 7 different
# types of storms but I have 8 options: the first will show all of them, while from the second to the last option, only
# one type at the time will be shown on the map
buttons= list_updatemenus_buttons,
type= 'dropdown',
# direction where the drop-down expands when opened
direction = 'down',
# positional arguments
x = -0.07,
xanchor = 'right',
y = 0.90,
yanchor = 'bottom',
# fonts and border
bgcolor = '#EEEEEE',
bordercolor = '#FFFFFF',
font = dict(color = '#000000', size=14)
)
])
#basic layout info for the plot
layout = dict(
height = 600, width= 1000,
plot_bgcolor='rgba(240,240,240,0.8)', title_x=0.55, title_y= 0.90,
title = '<b>Trend of number of tests performed</b>',
xaxis= dict(title= '<b>Date</b>', tickangle= -45), yaxis= dict(title= ''),
# top, bottom, left and right margins
# margin = dict(t = 0, b = 0, l = 0, r = 0),
font = dict(color = 'rgba(0, 0, 0, 0.8)'),
# paper_bgcolor = '#000000',
# showlegend=False,
)
# list_updatemenus.append(list_updatemenus_1)
# assign the list of dictionaries to the layout dictionary
layout['updatemenus'] = list_updatemenus
figure = dict(data = data, layout = layout)
iplot(figure)
data = []
#preparing data for the scatter plot country-wise
for i in range(country_list_test.shape[0]):
country_name= country_list_test[i]
test_df_grp_country= test_df_grp.loc[test_df_grp['country'] == country_name,:]
# test_df_grp_country= test_df_grp_country.dropna(how="any", subset=["Cumulative total"])
# test_df_
# deaths_country_time= deaths_country_df.transpose().reset_index().iloc[3:,:]
# deaths_country_time.columns= ['Date', 'Number of deaths']
# deaths_country_df= deaths_country.loc[deaths_country['country'] == country_name,:]
# active_cases_country_df= active_cases_country.loc[active_cases_country['country'] == country_name,:]
# recovered_cases_country_df= recovered_cases_country.loc[recovered_cases_country['country'] == country_name,:]
country_trace = go.Scatter(
x = test_df_grp_country['Date'],
y = test_df_grp_country["New tests"],
name = country_name,
text= country_name+ '<br><br>',
# marker = dict(color = 'rgba(80, 26, 80, 0.8)', opacity= 1),
mode= 'lines+markers', hoverinfo= "text+x+y"
)
data.append(country_trace)
#designing dropdown menu for the layout of plot
list_updatemenus_buttons= []
title_text= '<b>Trend of new tests performed</b>'
default_dict= dict(label = "All",
method = 'update',
args = [{'visible': [True]*n_countries_test},
{'title': title_text}])
list_updatemenus_buttons.append(default_dict)
for i in range(country_list_test.shape[0]):
country_name= country_list_test[i]
title_text= '<b>Trend of new tests performed</b>'
visible = [False] * n_countries_test
visible[i] = True
temp_dict = dict(label = country_name,
method = 'update',
args = [{'visible': visible},
{'title': title_text}, {'font':11}])
list_updatemenus_buttons.append(temp_dict)
# drop-down 2: select type of storm event to visualize
list_updatemenus = list( [dict(
# for each button I specify which dictionaries of my data list I want to visualize. Remember I have 7 different
# types of storms but I have 8 options: the first will show all of them, while from the second to the last option, only
# one type at the time will be shown on the map
buttons= list_updatemenus_buttons,
type= 'dropdown',
# direction where the drop-down expands when opened
direction = 'down',
# positional arguments
x = -0.07,
xanchor = 'right',
y = 0.90,
yanchor = 'bottom',
# fonts and border
bgcolor = '#EEEEEE',
bordercolor = '#FFFFFF',
font = dict(color = '#000000', size=14)
)
])
#basic layout info for the plot
layout = dict(
height = 600, width= 1000,
plot_bgcolor='rgba(240,240,240,0.8)', title_x=0.55, title_y= 0.9,
title = '<b>Trend of new tests performed</b>'.format(last_updated_date),
xaxis= dict(title= '<b>Date</b>', tickangle= -45), yaxis= dict(title= ''),
# top, bottom, left and right margins
# margin = dict(t = 0, b = 0, l = 0, r = 0),
font = dict(color = 'rgba(0, 0, 0, 0.8)'),
# paper_bgcolor = '#000000',
# showlegend=False,
)
# list_updatemenus.append(list_updatemenus_1)
# assign the list of dictionaries to the layout dictionary
layout['updatemenus'] = list_updatemenus
figure = dict(data = data, layout = layout)
iplot(figure)
test_df_grp_country= test_df_grp.groupby(by="country")["Total tests per million"].max()
test_df_grp_country= test_df_grp_country.reset_index()
test_df_grp_country["Total tests per million"]= test_df_grp_country["Total tests per million"]
test_df_grp_country_sort= test_df_grp_country.sort_values(by="Total tests per million", ascending= False)
data= go.Bar(
y = test_df_grp_country_sort.iloc[:80,:]['country'],
x = test_df_grp_country_sort.iloc[:80,:]["Total tests per million"],
name = country_name,
marker = dict(color = 'rgba(100, 216, 210, 0.8)', opacity= 0.9,
line=dict(color='rgb(0,0,0)',width=1.5)),
text= test_df_grp_country_sort.iloc[:80,:]["Total tests per million"],
textposition='outside', hoverinfo= 'x+y',
orientation= 'h'
)
layout = dict(
height = 1800, width= 1500,
plot_bgcolor='rgba(240,240,240,0.8)',
title = '<b>Number of tests per million</b>', title_x=0.37, title_y= 0.96,
xaxis= dict(title= ''), yaxis= dict(title= '', autorange="reversed"),
# top, bottom, left and right margins
# margin = dict(t = 0, b = 0, l = 0, r = 0),
font = dict(color = 'rgba(0, 0, 0, 0.8)'),
# paper_bgcolor = '#000000',
showlegend=False,
)
figure = dict(data = data, layout = layout)
iplot(figure)